window.onload = initalizePage;
window.onunload = function() {}

function initalizePage() {
/*
	var productsDiv = document.getElementById('products');
	var productsDivRealLength = 0
	//var avgNodeWid = 0;
	
	for (var i = 0; i<productsDiv.childNodes.length; i++) {
		if (productsDiv.childNodes[i].nodeType == 1) {
			productsDivRealLength++;
			//avgNodeWid = productsDiv.childNodes[i].max-width;
		}
	}
	if (productsDivRealLength >= 3) {
		productsDiv.style.width = ((productsDivRealLength * 15) + 2) + 'em';
		//document.writeln(avgNodeWid + 'px');
	}
*/
	/*
	document.getElementById('compareLink').onclick = handleCompareLinkClick;

	var select_sort = document.getElementById('sort');
	select_sort.onchange = function() { updateProducts('sort', this.value); }
	var select_itemPerPage = document.getElementById('itemNum');
	select_itemPerPage.onchange = function() { updateProducts('per_page', this.value); }

	var select_sort2 = document.getElementById('sort2');
	select_sort2.onchange = function() { window.location.search = generate_search_str('sort', this.value); };
	var select_itemPerPage2 = document.getElementById('itemNum2');
	select_itemPerPage2.onchange = function() { window.location.search = generate_search_str('per_page', this.value); };

	*/

}

function handleCompareLinkClick() {
// 	for (var intgr in toCompareProducts) {
// 		document.writeln(toCompareProducts[intgr]);
// 	}
	window.location = '/24/only_2_m/productCompare?compareList=' + toCompareProducts.join('!M');
	return false;
}

function pickme() {
	toCompareProducts.push(this.value);
}


function moveHandler(e) {
	var posx = 0;
	var posy = 0;
	if (!e) { var e = window.event; }
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
// 		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
// 		y:ev.clientY + document.body.scrollTop  - document.body.clientTop

	}
	img.style.top = (posy - 20) + 'px';
	img.style.left = (posx + 10) + 'px';
}


function handleRequest(req) {
	var productsDiv = document.getElementById('products');

	productsDiv.innerHTML = req.responseText;
	productsDiv.style.opacity = 1;
	window.onmousemove = false;
	img.style.display = 'none';

}

function duringRequest() {
	var productsDiv = document.getElementById('products');
	productsDiv.style.opacity = 0.3;
	if (!img) {
		img = document.createElement('img');
		img.src = '/24/only_2_m/javascripts/public/ax.gif';
		img.style.position = 'absolute';
		body = document.getElementsByTagName('body')[0];
		body.appendChild(img);
	}
	else {
		img.style.display = 'block';
	}
	window.onmousemove = moveHandler;
}

function updateProducts(urlOpt, newVal) {
	var productsDiv = document.getElementById('products');
	var url = window.location.pathname + generate_search_str(urlOpt, newVal) + '&reqType=ajax';

	sendRequest(url,handleRequest, duringRequest);
	
}


function generate_search_str(urlOpt, newVal) {
	search_arr = window.location.search.substring(1).split('&');

	var niceArr = [];
	for (var intgr in search_arr) {
		var keyVal = search_arr[intgr].split('=');
		niceArr[keyVal[0]] = keyVal[1];
	}
	niceArr[urlOpt] = newVal;

	var out_search_arr = []
	for (var key in niceArr) {
		var temp_str = key + '=' + niceArr[key];
		out_search_arr.push(temp_str);
	}
	return ('?' + out_search_arr.join('&'));
}


// ----------------------------------------------------------

function sendRequest(url,callback,callduring,postData) {
	var req = getXhr();
	if (!req) { 
		alert("Sorry, couldn't create an XMLHttpRequest");
		return; 
	}
	var method = (postData) ? 'POST' : 'GET';
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData) { req.setRequestHeader('Content-type','application/x-www-form-urlencoded'); }
	req.onreadystatechange = function () {
		if (req.readyState != 4) { 
			callduring();
			return;
		}
		if (req.status != 200 && req.status != 304) {
			alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) { return; }
	req.send(postData);
}

var XMLHttpPossibilities = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function getXhr() {
	for (var i=0;i<XMLHttpPossibilities.length;i++) {
		try { xhr = XMLHttpPossibilities[i](); }
		catch (e) { continue; }
		break;
	}
	return xhr;
}

// ----------------------------------------------------------